package Solution;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-20
 * Time: 16:49
 */
//https://leetcode.cn/problems/roman-to-integer/description/
class Solution5 {
    public int romanToInt(String s) {
        //哈希思想
        int[] arr = new int[26];
        arr['I' - 'A'] = 1;
        arr['V' - 'A'] = 5;
        arr['X' - 'A'] = 10;
        arr['L' - 'A'] = 50;
        arr['C' - 'A'] = 100;
        arr['D' - 'A'] = 500;
        arr['M' - 'A'] = 1000;
        int sl = s.length();
        int num = 0;
        for (int i = 0; i < sl; i++) {
            if (i < sl - 1 && arr[s.charAt(i) - 'A'] < arr[s.charAt(i + 1) - 'A']) {
                num += arr[s.charAt(i + 1) - 'A'] - arr[s.charAt(i) - 'A'];
                i++;
            } else {
                num += arr[s.charAt(i) - 'A'];
            }
        }
        return num;
    }
}
